package com.liuguilin.contentprovidersampler;

/*
 *  项目名：  ContentProviderSampler 
 *  包名：    com.liuguilin.contentprovidersampler
 *  文件名:   ProviderActivity
 *  创建者:   LGL
 *  创建时间:  2016/10/20 13:55
 *  描述：    ContentProvider类
 */

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class ProviderActivity extends AppCompatActivity{

    public static final String TAG = "ProviderActivity";

    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_provider);

        Uri bookUri = Uri.parse("content//com.liuguilin.contentprovidersampler.BookProvider/book");

        ContentValues values = new ContentValues();
        values.put("_id",6);
        values.put("name","程序设计的艺术");
        getContentResolver().insert(bookUri,values);
        Cursor bookCursor = getContentResolver().query(bookUri,new String[]{"_id","name"},null,null,null);
        while (bookCursor.moveToNext()){
            Book book = new Book();
            book.id = bookCursor.getInt(0);
            book.name = bookCursor.getString(1);
        }
        bookCursor.close();

        Uri userUri = Uri.parse("content//com.liuguilin.contentprovidersampler.BookProvider/user");
        Cursor userCursor = getContentResolver().query(userUri,new String[]{"_id","name","sex"},null,null,null);
        while (userCursor.moveToNext()){
            User user = new User();
            user.id= userCursor.getInt(0);
            user.name = userCursor.getString(1);
            user.isMale = userCursor.getInt(2) == 1;
        }
        userCursor.close();

    }
}
